package com.yongninger.adapter;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;

public class MyCursorAdpter extends CursorAdapter {
 private int columnIndex;
 SQLiteDatabase db;
	public MyCursorAdpter(Context context, Cursor c,int columnIndex,SQLiteDatabase db) {
		super(context, c);
		this.columnIndex=columnIndex;
		this.db=db;
	}

	@Override
	public void bindView(View arg0, Context arg1, Cursor arg2) {
		 ((TextView) arg0).setText(arg2.getString(columnIndex));     
		
	}
	@Override     
	     public String convertToString(Cursor cursor) {     
	         return cursor.getString(columnIndex);     
	     } 
	@Override
	public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
		final LayoutInflater inflater = LayoutInflater.from(arg0);     
		         final TextView view = (TextView) inflater.inflate(     
	                android.R.layout.simple_dropdown_item_1line, arg2, false);     
		        view.setText(arg1.getString(columnIndex));     
		         return view;     

	}
	 @Override     
	      public Cursor runQueryOnBackgroundThread(CharSequence constraint) {    
		 String[] columns={"_id","ccode","cname"};
	         if (constraint != null) {     
	             String selection = "cname like \'%" + constraint.toString() +"%\'";   
	             Cursor c = db.query("customer_info", columns, selection, null, null, null, null);   
	            return c;     
	          }     
	         else {     
	              return null;     
	          }     
	     }     


}
